//
//  MarketDetailTableViewCell.m
//  bondwatch_purecode
//
//  Created by Jason Yuan on 5/29/15.
//  Copyright (c) 2015 Sumridge. All rights reserved.
//

#import "MarketDetailTableViewCell.h"

#define DescriptionColor [UIColor colorWithRed:84.0/255.0 green:127.0/255.0 blue:36.0/255.0 alpha:1.0]
#define TargetColor [UIColor colorWithRed:227.0/255.0 green:65.0/255.0 blue:255.0/255.0 alpha:1.0]

#define DescriptionFont [UIFont fontWithName:@"Arial-BoldMT" size:16.0];

@implementation MarketDetailTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        descriptionLabel = [[UILabel alloc] init];
        descriptionLabel.textColor = DescriptionColor;
        descriptionLabel.font = DescriptionFont;
        descriptionLabel.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tapDescription = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapDescription:)];
        [descriptionLabel addGestureRecognizer:tapDescription];
        
        bidLabel = [[UILabel alloc] init];
        [bidLabel setTextColor:[UIColor blackColor]];
        bidLabel.textAlignment = NSTextAlignmentLeft;
        
        targetLabel = [[UILabel alloc] init];
        [targetLabel setTextColor:TargetColor];
        
        UITapGestureRecognizer *tapTargetLabel = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTargetLabel:)];
        [targetLabel addGestureRecognizer:tapTargetLabel];
        
        [self addSubview:descriptionLabel];
        [self addSubview:bidLabel];
        [self addSubview:targetLabel];
    }
    return self;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
- (void)drawRect:(CGRect)rect {
    CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor); CGContextFillRect(context, rect);

    //上分割线，
    CGContextSetStrokeColorWithColor(context, SUMRIDGECOLOR.CGColor); CGContextStrokeRect(context, CGRectMake(0, -1, rect.size.width, 1));
//    //下分割线
//    CGContextSetStrokeColorWithColor(context, [UIColor blackColor].CGColor); CGContextStrokeRect(context, CGRectMake(0, rect.size.height, rect.size.width, 1));
}

- (void)setUIFrame {
    if ([UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationLandscapeLeft ||
        [UIApplication sharedApplication].statusBarOrientation == UIDeviceOrientationLandscapeRight) {
        [descriptionLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(10);
            make.centerY.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(200.0, 20.0));
        }];
        
        [bidLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(self);
            make.left.equalTo(@[descriptionLabel]).offset(200);
            make.right.equalTo(@[targetLabel]).offset(-130);
        }];
        
        [targetLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-10);
            make.centerY.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(130.0, 20.0));
        }];
    } else {
        [descriptionLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(10);
            make.top.equalTo(self).offset(5);
            make.size.mas_equalTo(CGSizeMake(200.0, 20.0));
        }];
        
        [bidLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).offset(10);
            make.top.equalTo(self).offset(30);
            make.size.mas_equalTo(CGSizeMake(130.0, 20.0));
        }];
        
        [targetLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).offset(-10);
            make.top.equalTo(self).offset(30);
            make.size.mas_equalTo(CGSizeMake(130.0, 20.0));
        }];
    }
}

- (void)setLabelText:(NSDictionary *)data offerData:(NSDictionary *)offerData {
    self.infoData = data;
    NSString *description = [data objectForKey:@"description"];
    NSNumber *bestQty = [offerData objectForKey:@"bestQty"];
    NSNumber *bestPrice = [offerData objectForKey:@"bestPrice"];
    NSNumber *target = [data objectForKey:@"target"];
    NSNumber *type = [data objectForKey:@"type"];
    DDLogVerbose(@"description: %@\nbestQty: %@, bestPrice: %@\ntarget: %@", description, bestQty, bestPrice, target);
    
    descriptionLabel.text = description;
    if (bestQty == nil ||
        [bestQty isEqual:[NSNull null]]) {
        bidLabel.text = @"";
    } else {
        bidLabel.text = [NSString stringWithFormat:@"%@M@%.3f", bestQty, [bestPrice floatValue]];
    }
    if (target == nil ||
        [target isEqual:[NSNull null]]) {
        targetLabel.text = @"";
    } else {
        float changeValue = 0.0;
        
        if ([type intValue] == 0) {
            changeValue = fabs([target floatValue] - [bestPrice floatValue]);
        } else if ([type intValue] == 3) {
            NSNumber *bestYield = [offerData objectForKey:@"bestYield"];
            if (bestYield == nil ||
                [bestYield isEqual:[NSNull null]]) {
                ;
            } else {
                changeValue = fabs([target floatValue] - [bestYield floatValue]);
            }
        } else {
            NSNumber *bestSpread = [offerData objectForKey:@"bestSpread"];
            if (bestSpread == nil ||
                [bestSpread isEqual:[NSNull null]]) {
                ;
            } else {
                changeValue = fabs([target floatValue] - [bestSpread floatValue]);
            }
        }

        NSNumber *threshold = [data objectForKey:@"threshold"];
        targetLabel.text = [NSString stringWithFormat:@"%.3f", [target floatValue]];
        
        if (changeValue < [threshold floatValue]) {
            [UIView animateWithDuration:1.0 delay:0.f options:(UIViewAnimationOptionAutoreverse| UIViewAnimationOptionRepeat)
                             animations:^{
                                 targetLabel.alpha=0.1f;
                             } completion:^(BOOL finished){
                                 targetLabel.alpha=1.0f;
                             }];
            
            [UIView setAnimationDelegate:self];
            [UIView setAnimationDidStopSelector:@selector(flashingDidStop:finished:context:)];
            
        }
    }
}

//with the callback method
- (void)flashingDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context
{
    //code to execute in your case
    targetLabel.alpha = 1.0f;
}

- (void)tapDescription:(id)sender {
    [self.delegate tapDescription:self.infoData];
}

- (void)tapTargetLabel:(id)sender {
    [self.delegate tapTarget:self.infoData];
}

@end
